/////////////////////////////////////////////////////////////////////////////
//
// XepdtConnectionListener.cpp
//
//
/////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "XepdtConnectionListener.h"
#include "XepdtClient.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CXepdtConnectionListener

/////////////////////////////////////////////////////////////////////////////
// CXepdtConnectionListener constructor and destructor

CXepdtConnectionListener::CXepdtConnectionListener()
{
	m_pResult = gloox::ConnNoError;
}

CXepdtConnectionListener::CXepdtConnectionListener( CXepdtClient* pClient )
{
	ASSERT( pClient );

	m_pResult = gloox::ConnNoError;
	m_pClient = pClient;
}

CXepdtConnectionListener::~CXepdtConnectionListener()
{
}

/////////////////////////////////////////////////////////////////////////////
// CXepdtConnectionListener override implementation

void CXepdtConnectionListener::onConnect()
{
	ASSERT( m_pClient != NULL );
	if ( m_pClient != NULL )
	{
		m_pClient->OutputLogInfo( "Connecting Xepdt server !", CXepdtLogConsole::TEXT_WHITE );
	}

}

void CXepdtConnectionListener::onDisconnect( gloox::ConnectionError e )
{
	ASSERT( m_pClient != NULL );

	switch ( e )
	{
	case gloox::ConnNoError :
		m_pClient->OutputLogInfo( "Not really an error. Everything went just fine.", CXepdtLogConsole::TEXT_WHITE );
		break;

	case gloox::ConnStreamError :
		m_pClient->OutputLogInfo( "A stream error occured. The stream has been closed.", CXepdtLogConsole::TEXT_WHITE );
		break;

	case gloox::ConnStreamClosed :
		m_pClient->OutputLogInfo( "The stream has been closed graciously.", CXepdtLogConsole::TEXT_WHITE );
		break;

	case gloox::ConnIoError :
		m_pClient->OutputLogInfo( "An I/O error occured.", CXepdtLogConsole::TEXT_WHITE );
		break;

	case gloox::ConnOutOfMemory :
		m_pClient->OutputLogInfo( "Out of memory. Uhoh.", CXepdtLogConsole::TEXT_WHITE );
		break;

	case gloox::ConnNoSupportedAuth :
		m_pClient->OutputLogInfo( "The auth mechanisms the server offers are not supported" \
									"or the server offered no auth mechanisms at all.", CXepdtLogConsole::TEXT_WHITE );
		break;

	case gloox::ConnTlsFailed :
		m_pClient->OutputLogInfo( "The server's certificate could not be verified.", CXepdtLogConsole::TEXT_WHITE );
		break;

	case gloox::ConnAuthenticationFailed :
		m_pResult = gloox::ConnAuthenticationFailed;
		m_pClient->OutputLogInfo( "Authentication failed. Username/password wrong or account does not exist.", CXepdtLogConsole::TEXT_WHITE );
		break;

	case gloox::ConnUserDisconnected :
		m_pClient->OutputLogInfo( "The user (or higher-level protocol) requested a disconnect.", CXepdtLogConsole::TEXT_WHITE );
		break;

	case gloox::ConnNotConnected :
		m_pClient->OutputLogInfo( "There is no active connection.", CXepdtLogConsole::TEXT_WHITE );
		break;
	}
}

bool CXepdtConnectionListener::onTLSConnect( const struct gloox::CertInfo& )
{
	return true;
}
